//
//  TDMineController.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/21.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDMineController.h"
#import "TDMineIconHeadCell.h"
#import "TDMineInfoBasicCell.h"
#import "TBRUnLoginViewC.h"
#import "BaseNavigationController.h"
#import "TBRLoginController.h"
#import "TBRMineModel.h"
#import "TDMineSettingC.h"
#import "TDMineBasicInfoC.h"
#import "NetWorkRequest.h"
@interface TDMineController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)TBRUnLoginViewC *unLoginViewC;

@property (nonatomic,strong)TBRMineModel *shareMineInfoModel;
@end

@implementation TDMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    if ([[AppSetting shareAppSetting].isLogin isEqualToString:@"1"]) {
        self.unLoginViewC.view.hidden = YES;
        [self getAllMessageNet];
    }else {
        self.unLoginViewC.view.hidden = NO;
    }
}


- (void)setupUI {
    //添加tableView
    [self setupTableView];
    
    [self setupUnLoginC];
}


- (void)setupTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDMineIconHeadCell" bundle:nil] forCellReuseIdentifier:@"TDMineIconHeadCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDMineInfoBasicCell" bundle:nil] forCellReuseIdentifier:@"TDMineInfoBasicCellId"];
    self.tableView.showsVerticalScrollIndicator = NO;
}

- (void)setupUnLoginC {
    TBRUnLoginViewC *unLoginViewC = [[TBRUnLoginViewC alloc] init];
    self.unLoginViewC = unLoginViewC;
    [self.view addSubview:unLoginViewC.view];
    [self addChildViewController:unLoginViewC];
    [self.unLoginViewC.view makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.leading.trailing.offset(0);
    }];
    __weak typeof(self) weakSelf = self;
    unLoginViewC.unLoginViewBlock = ^{
        
        //跳转到登录页面----
        TBRLoginController *loginC = [[TBRLoginController alloc] init];
        BaseNavigationController *loginNav = [[BaseNavigationController alloc] initWithRootViewController:loginC];
        [weakSelf presentViewController:loginNav animated:YES completion:nil];
    };
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TDMineIconHeadCellId";
    if (indexPath.row == 0) {
        iden = @"TDMineIconHeadCellId";
    }else {
        iden = @"TDMineInfoBasicCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *urlStr = [NSString stringWithFormat:@"http://121.40.92.131/deepdriving/%@",[AppSetting shareAppSetting].photo];
        TDMineIconHeadCell *cellHead = (TDMineIconHeadCell *)cell;
        [cellHead.imv_photo sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"头像"]];
        cellHead.lbe_nickName.text = [HelpObject isBlankString:[AppSetting shareAppSetting].nickname] ? [AppSetting shareAppSetting].phone : [AppSetting shareAppSetting].nickname;
    }else {
        TDMineInfoBasicCell *infoBasicCell = (TDMineInfoBasicCell *)cell;
        TBRMineInfoBasicModel *basicModel = self.dataArray[indexPath.row];
        infoBasicCell.basicModel = basicModel;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.row == 0 ? 145 : 73;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        TDMineBasicInfoC *mineBasicInfoC = [[TDMineBasicInfoC alloc] init];
        mineBasicInfoC.shareMineInfoModel = self.shareMineInfoModel;
        [self.navigationController pushViewController:mineBasicInfoC animated:YES];
    }else {
        TBRMineInfoBasicModel *basicModel = self.dataArray[indexPath.row];
        if (!basicModel.className) {
            return;
        }
        if ([basicModel.className isEqualToString:@"TDMineSettingC"]) {
            __weak typeof(self) weakSelf = self;
            TDMineSettingC *settingC = [[TDMineSettingC alloc] init];
            settingC.mineSettingLogooutBlock = ^{
                weakSelf.unLoginViewC.view.hidden = NO;
                [weakSelf.navigationController popViewControllerAnimated:NO];
            };
            [self.navigationController pushViewController:settingC animated:YES];
            return;
        }
        Class ClassName = NSClassFromString(basicModel.className);
        UIViewController *viewC = [[ClassName alloc] init];
        [self.navigationController pushViewController:viewC animated:YES];
    }
}


// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    NSArray *tmpBasicArray = @[
                               @{@"messageId":@"myMessage",
                                 @"imageName":@"",
                                 @"titleName":@"消息",
                                 @"className":@"TBRNewNoticeListC"},
                               @{@"messageId":@"myMessage",
                                 @"imageName":@"",
                                 @"titleName":@"消息",
                                 @"className":@"TDMineMessageC"},
                               @{@"messageId":@"myMessage",
                                 @"imageName":@"",
                                 @"titleName":@"试驾券",
                                 @"className":@"TDTryDirveQuanC"},
                               @{@"messageId":@"myMessage",
                                 @"imageName":@"",
                                 @"titleName":@"设置",
                                 @"className":@"TDMineSettingC"},
                               @{@"messageId":@"myMessage",
                                 @"imageName":@"",
                                 @"titleName":@"用户指南",
                                 @"className":@"TBRNewNoticeListC"},
                               @{@"messageId":@"myMessage",
                                 @"imageName":@"",
                                 @"titleName":@"邀请好友",
                                 @"className":@"TBRNewNoticeListC"},
                               @{@"messageId":@"myMessage",
                                 @"imageName":@"",
                                 @"titleName":@"意见反馈",
                                 @"className":@"TBRNewNoticeListC"},
                               ];
    self.dataArray = [NSArray yy_modelArrayWithClass:[TBRMineInfoBasicModel class] json:tmpBasicArray].mutableCopy;
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    
}


- (void)getAllMessageNet {
    // MARK: - 登录操作--------------
    NSDictionary *dic = @{};
    __weak typeof(self) weakSelf = self;
    [NetWorkRequest postNetRequestData:GetMemberMessage parmeters:dic response:^(NSDictionary *response) {
        if (response) {
            //保存数据---进行一系列的操作----------
            [AppSetting shareAppSetting].photo = response[@"data"][@"photo"];
            [AppSetting shareAppSetting].nickname = response[@"data"][@"nickname"];
            [AppSetting shareAppSetting].updatetime = response[@"data"][@"updatetime"];
            [AppSetting shareAppSetting].phone = response[@"data"][@"phone"];
             weakSelf.shareMineInfoModel = [TBRMineModel yy_modelWithJSON:response[@"data"]];
            [weakSelf.tableView reloadData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"获取个人信息失败" isSucceed:NO];
    }];
}

@end
